package com.hellogood.yaohuo.server.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.db.DBConnection;
import com.hellogood.yaohuo.shared.data.CallDTO;
import com.hellogood.yaohuo.shared.entity.YsDetail;
import com.hellogood.yaohuo.shared.entity.YsForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;

public class YsFormDAO extends FormDAO {
	Logger log = Logger.getLogger(this.getClass());

	public String createYanshou(YsForm form, boolean isModify) {
		String result = "";
		List<String> sqls = new ArrayList<String>();
		if (isModify) {
			String sql = "DELETE FROM YsForm WHERE ysBillNo = '" + form.getYsBillNo() + "'";
			sqls.add(sql);
			sql = "DELETE FROM YsDetail WHERE ysBillNo = '" + form.getYsBillNo() + "'";
			sqls.add(sql);
		}
		String sql = "INSERT INTO YsForm(ysBillNo,ysDate,orgCode,vendorCode,vendorName,counts,cost,netCost,total," + "staffCode,staffName,rzDate,remark,createTime) VALUES('" + form.getYsBillNo()
				+ "',CONVERT(varchar(100), GETDATE(), 23),'" + form.getOrgCode() + "','" + form.getVendorCode() + "','" + form.getVendorName() + "'," + form.getCounts() + "," + form.getCost() + ","
				+ form.getNetCost() + "," + form.getTotal() + ",'" + form.getStaffCode() + "','" + form.getStaffName() + "','" + form.getRzDate() + "','" + form.getRemark() + "',getDate())";
		sqls.add(sql);

		List<YsDetail> list = form.getList();
		if (list != null && (!list.isEmpty())) {
			for (YsDetail d : list) {
				sql = "INSERT INTO YsDetail(ysBillNo,pluCode,pluName,barCode,size_s,size_m,size_l,size_xl,size_xxl,size_xxxl,size_avg,depCode,depName,Bra,Color,jPrice,netJPrice,price,counts,"
						+ "cost,netCost,total,rzDate,remark,orgCode) VALUES('"
						+ d.getBillNo()
						+ "','"
						+ d.getPluCode()
						+ "','"
						+ d.getPluName()
						+ "','"
						+ d.getBarCode()
						+ "',"
						+ d.getSize_s()
						+ ","
						+ d.getSize_m()
						+ ","
						+ d.getSize_l()
						+ ","
						+ d.getSize_xl()
						+ ","
						+ d.getSize_xxl()
						+ ","
						+ d.getSize_xxxl()
						+ ","
						+ d.getSize_avg()
						+ ",'"
						+ d.getDepCode()
						+ "','"
						+ d.getDepName()
						+ "','"
						+ d.getBra()
						+ "','"
						+ d.getColor()
						+ "',"
						+ d.getjPrice()
						+ ","
						+ d.getPrice()
						+ ","
						+ d.getPrice()
						+ ","
						+ d.getCounts()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getCost()
						+ ","
						+ d.getTotal()
						+ ",'"
						+ d.getRzDate() + "','" + d.getRemark() + "','" + d.getOrgCode() + "')";
				sqls.add(sql);
			}
		}
		try {
			executeBatch(sqls);
		} catch (DatabaseException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return e.getMessage();
		}
		return result;
	}

	public String rz(String bill) throws DatabaseException {
		CallDTO dto = new CallDTO();
		String sql = "UPDATE ysForm SET rzDate = CONVERT(VARCHAR(10),getDate(),120) WHERE ysBillNo = '" + bill + "'";
		String call = "{CALL ysrz(?,?)}";
//		String call = "{EXEC ysrz @ysbillNo = '" + bill + "',@sysdb='" + DBConnection.hxDbName + "'}";
		dto.setBill(bill);
		dto.setCall(call);
		dto.setSql(sql);
		dto.setHxdbName(DBConnection.hxDbName);
		return super.callProcedure(dto);
	}
}
